from haystack import indexes

from orders.models import OrderInfo


class DocsIndex(indexes.SearchIndex, indexes.Indexable):

    text = indexes.CharField(document=True, use_template=True)
    ordergoods_set = indexes.MultiValueField()

    def get_model(self):
        return OrderInfo

    def prepare_ordergoods_set(self, object):
        return [goods.goods.name for goods in object.ordergoods_set.all()]

    def index_queryset(self, using=None):
        """Used when the entire index for model is updated."""
        return self.get_model().objects.all()